package com.sky.service.dish;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.dish.DishDTO;
import com.sky.dto.dish.DishPageDTO;
import com.sky.dto.dish.DishUpdateDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorsMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.vo.DishPageVO;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 褚金龙
 * @Date 2024/9/1 21:22
 */
@Slf4j
@Service
public class DishServiceImpl implements DishService{


    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorsMapper flavorsMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 分页查询菜品信息
     * @param dishPageDTO
     * @return
     */
    @Override
    public PageResult dishPageService(DishPageDTO dishPageDTO) {
        PageHelper.startPage(dishPageDTO.getPage(),dishPageDTO.getPageSize());
        Page<DishPageVO> dishes = dishMapper.dishPageMapper(dishPageDTO);

           return new PageResult(dishes.getTotal(),dishes);
    }

    /**
     * 套餐、菜品分类数据查询
     * @return
     */
    @Override
    public PageResult dishTypeService(Integer type) {
       // log.info("type{}",type);
        Page<DishVO> page=  dishMapper.dishTypeMapper(type);
     // log.info("page{}",page);
        return new PageResult(page.getTotal(),page);
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional  //添加事务  因为是操作两张表，要么全部成功要么全部失败，必须还在启动类上加@EnableTransactionManagement//开启注解方式的事务管理
    @Override
    public void addDishService(DishDTO dishDTO) {
        //1.向菜品表插入数据
        //传一个dish对象，让其分开操作
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.dishAddMapper(dish);
        //2.向口味表插入数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //拿到dishID
        Long dishId = dish.getId();
        //首先判断是否为空
        if(flavors!=null && flavors.size()>0){
            // 向数据库插入数据
            // 获取菜品表id,拿到添加之后的id

            flavors.forEach((item) ->{
                item.setDishId(dishId);
            });
            flavorsMapper.addFlavors(flavors);
        }

        //向套餐菜品表写入菜品id
      //  setmealDishMapper.insert(dishId);

    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void deleteAll(List<Long> ids) {
        //1.判断当前菜品是否能够删除->是否存在起售中的菜品
        for (Long id : ids) {
         Dish dish=   dishMapper.getById(id);
         // 判断菜品是起售还是停售
           if(dish.getStatus()== StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        }
        //2.判断当前菜品是否能够删除->是否被套餐关联了？
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds!=null && setmealIds.size()>0){
            //当前菜品被套餐关联了，不能删除
            throw  new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //3. 删除菜品表中的菜品数据

            dishMapper.deleteDish(ids);
            //根据菜品的id删除对应的口味数据


        //4.删除菜品关联的口味数据
        flavorsMapper.deleteFlavors(ids);
    }





    /**
     * 修改菜品状态
     * @param status
     * @param id
     */

    @Override
    public void statusUpdateService(Integer status, Integer id) {
         dishMapper.statusUpdate(status,id);
    }


    /**
     * 根据id回显菜品
     * @param id
     */
    @Override
    public  DishDTO  getById(Long id) {
      DishDTO dishDTO=  dishMapper.queryDish(id);

        List<DishFlavor> DishFlavor =flavorsMapper.DishFlavor(id);
        log.info("{}",DishFlavor);
        dishDTO.setFlavors(DishFlavor);
        log.info("dishDTO{}",dishDTO);
      return dishDTO;
    }


    /**
     * 修改菜品数据
     * @param  dishDTO
     */
    @Transactional
    @Override
    public void updateDishService(DishDTO dishDTO) {
         DishUpdateDTO dishUpdateDTO=new DishUpdateDTO();
         BeanUtils.copyProperties(dishDTO,dishUpdateDTO);

        dishMapper.updateDishMapper(dishUpdateDTO);
        log.info("dishDTO.getFlavors(){}",dishDTO.getFlavors());
       flavorsMapper.updateFlavorMapper(dishDTO.getFlavors());

    }
}
